package cn.ljn.org.service.impl;

import cn.ljn.basic.eum.ShopStateEnum;
import cn.ljn.basic.service.impl.BaseServiceImpl;
import cn.ljn.basic.util.BaiduAuditUtils;
import cn.ljn.basic.util.BusinessException;
import cn.ljn.basic.util.SendEmailUtil;
import cn.ljn.org.domain.Employee;
import cn.ljn.org.domain.Shop;
import cn.ljn.org.domain.ShopAuditLog;
import cn.ljn.org.dto.ShopAdminDto;
import cn.ljn.org.dto.ShopAuditDto;
import cn.ljn.org.dto.ShopSettlementDto;
import cn.ljn.org.mapper.EmployeeMapper;
import cn.ljn.org.mapper.ShopAuditLogMapper;
import cn.ljn.org.mapper.ShopMapper;
import cn.ljn.org.service.ShopService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;



    @Autowired
    private ShopAuditLogMapper auditLogMapper;



    /**
     * 店铺入驻
     *
     * @param dto
     */
    @Override
    public void settlement(ShopSettlementDto dto) {
        // 参数校验 ctrl+alt+m
        this.verify(dto);
        // 保存数据
        this.save(dto);
    }
    /**
     * 店铺审核通过
     */
    @Override
    public void auditPass(ShopAuditDto dto) {
        extracted(dto,ShopStateEnum.WAIT_ACTIVE.getCode() );
        SendEmailUtil.EnterAuditPass("1045871582@qq.com",dto.getShopId(),"1045871582@qq.com");
    }
    @Override
    public void activity(Long id) {
        // 状态为已激活
        Shop shop = new Shop();
        shop.setState(3);
        shop.setId(id);
        shopMapper.update(shop);
    }


    /**
     * 驳回
     * @param dto
     */
    @Override
    public void reject(ShopAuditDto dto) {
        extracted(dto,ShopStateEnum.NO_AUDIT.getCode() );
        // 1.创建复杂邮件对象
        SendEmailUtil.EnterReject("1045871582@qq.com", dto.getNote(),dto.getShopId(),"1045871582@qq.com");
    }


    // 提交 通过/驳回 修改参数并保存进日志
    public void extracted(ShopAuditDto dto,int code) {
        Shop shop = new Shop();
        shop.setId(dto.getShopId());
        shop.setState(code);
        shopMapper.update(shop);
        // 保存日志
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setShopId(dto.getShopId());
        shopAuditLog.setNote(dto.getNote());
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(-1);
        auditLogMapper.add(shopAuditLog);
    }

    private void save(ShopSettlementDto dto) {
        // 6.添加数据
        // 把dto的字段取出来赋值给shop对象
        Shop shop=  dto.getShop();
        shopMapper.add(shop);
        // 添加emp对象
        // 把dto中的字段取出来 赋值给emp对象
        Employee emp = dto.getEmp();
        emp.setShopId(shop.getId());
        employeeMapper.add(emp);
        // shop里面没有adminId
        Shop shop1 = new Shop();
        shop1.setId(shop.getId());
        shop1.setAdminId(emp.getId());
        shopMapper.update(shop1);

    }

    private void verify(ShopSettlementDto dto) {
        // 1.参数校验

//        // 1.5手机号验证
        Pattern p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
        Matcher m = p.matcher(dto.getAdmin().getPhone());
        if (!m.matches()){
            throw new BusinessException("用户手机号格式不正确！");
        }
        // 2.两次密码是否一致
        ShopAdminDto admin = dto.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BusinessException("两次密码不一致！");
        }
        // 3.店铺是否注册过
        // 根据店铺名称去查询
        Shop shop = shopMapper.findByName(dto.getName());
        if (null != shop) {
            // 当前存在了该店铺
            throw new BusinessException("店铺已存在，请滚去登陆！");
        }
        // 4.当前管理员是否已经管理店铺
        // 根据用户名称去查询用户数据
        Employee employee = employeeMapper.findByName(admin.getUsername());
        if (null != employee && null != employee.getShopId()) { //他是店铺的人
            Shop shopTmp = shopMapper.findByAdminId(employee.getId());
            if (null != shopTmp) { // 这个人就是店长！
                throw new BusinessException("换个人！");
            }
        }
        // 百度AI审核
        if (!BaiduAuditUtils.TextCensor(dto.getName())){
            throw new BusinessException("店铺名称不合规");
        }
        if (!BaiduAuditUtils.ImgCensor(dto.getLogo())){
            throw new BusinessException("店铺Logo不合规");
        }
    }
}
